<template>
	<view class="content">
		<view class="nav-box flexWrap">
			<view class="nav-item flexWarpCenterColumn" v-for="(item,index) in navList" :key="index" :class="{active:currmentIndex==index}" @tap="changeNav(index)">
				<text>{{item.name}}</text>
				<text class="line"></text>
			</view>
		</view>
		<!-- <view class="flexWrapNo">
			<input placeholder="售后id" v-model="aftersalesId" style="color: #fff;"/>
			<button @tap="jumpAftersales()">跳转</button>
		</view> -->
		<view class="shop-box" v-for="(item,index) in orderList" :key="index" @tap="goPage('/pages/shop/shopOrderDetails?orderId='+item.order_sn)">
			<view class="top-box flexWrap">
				<view class="date">{{item.createtime}}</view>
				<block  v-if="item.paystate==0">
					<text class="status" v-if="item.orderstate==0">待支付</text>
					<text class="status" v-if="item.orderstate==1">已取消</text>
					<text class="status" v-if="item.orderstate==2">已失效</text>
				</block>
				<block v-else>
					<block v-if="item.orderstate==0">
						<text class="status" v-if="item.shippingstate==0">待发货</text>
						<text class="status" v-if="item.shippingstate==1">待收货</text>
						<text class="status" v-if="item.shippingstate==2">已收货</text>
					</block>
					<text class="status" v-if="item.orderstate==3">已完成</text>
					<text class="status" v-if="item.shippingstate==4">退货退款中</text>
				</block>	
			</view>
			<view class="shop-cont">
				<view class="shop-item flexWrapNo" v-for="(i,x) in item.order_goods" :key="x">
					<image :src="i.image" class="shop-img"></image>
					<view class="shop-info-box flexWarpColumn">
						<view class="info-box">
							<view>{{i.title}}</view>
							<view class="specs-box">{{i.attrdata||'默认'}} , x{{i.nums}}</view>
						</view>
						<view class="price">¥{{i.price}}</view>
					</view>
				</view>
			</view>
			<view class="total-box">
				共{{item.nums}}件 总额 <text class="price">¥ {{item.goodsprice}}</text>
			</view>
		</view>
		<uni-load-more v-if="showLoadStatus" :status="status" :size="14" :content-text="contentText" />
		<view class="empty-box" v-if="showEmpty">
			<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
		</view>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				navList:[
					{
						name:"全部",
						f:'',
						orderstate:'',
						paystate:'',
						shippingstate:''
					},
					{
						name:"待付款",
						f:1,
						orderstate:0,
						paystate:0,
						shippingstate:''
					},
					{
						name:"待发货",
						f:2,
						orderstate:0,
						paystate:1,
						shippingstate:0
					},
					{
						name:"待收货",
						f:3,
						orderstate:0,
						paystate:1,
						shippingstate:1
					},
					{
						name:"已完成",
						f:5,
						orderstate:3,
						paystate:1,
						shippingstate:''
					}
				],
				currmentIndex:0,
				//下拉刷新
				page: 1,
				size:10,
				showEmpty: true,
				showLoadStatus: false,
				more: true,
				contentText: {
					contentdown: '上拉加载更多~',
					contentrefresh: '加载中...',
					contentnomore: '已经到底了~~'
				},
				status: 'loading',
				orderList:[],
				f:"",
				orderstate:"",
				paystate:"",
				shippingstate:"",
				aftersalesId:""
			};
		},
		// 触底刷新
		onReachBottom() {
			if (this.more) {
				this.getData()
			}
		},
		// 下拉刷新
		onPullDownRefresh: function() {
			this.freshList();
			this.$tools.loadIng()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		onLoad() {
			this.getData();
		},
		methods:{
			jumpAftersales(){
				this.goPage('/shop-mall/afterSales?aftersalesId='+this.aftersalesId)
			},
			changeNav(index){
				this.currmentIndex=index
				this.f=this.navList[index].f
				this.orderstate=this.navList[index].orderstate
				this.paystate=this.navList[index].paystate
				this.shippingstate=this.navList[index].shippingstate
				this.freshList();
			},
			// 刷新列表
			freshList() {
				this.page = 1
				this.more = true
				this.status = 'loading'
				this.orderList = []
				this.getData();
			},
			getData(){
				let self=this
				this.$request.post(this.$api.shopOrderList, {
					page:this.page++,
					num:this.size,
					f:this.f,
					orderstate:this.orderstate,
					paystate:this.paystate,
					shippingstate:this.shippingstate
				}, {
					token:true
				}).then(res => {
					if (res.code == 1) {
						let {
							data = {}
						} = res;
						let newArray =data.data
						for (let i in newArray) {
							newArray[i].createtime = this.$tools.formatDate(newArray[i].createtime)
						}
						if (newArray.length == 0 && self.page == 2) {
							self.showEmpty = true
							self.showLoadStatus = false
							self.more = false // 不能再次请求了
							return false;
						} else {
							self.showLoadStatus = true
							self.more = true
							self.showEmpty = false
							self.status = 'loading'
						}
						if (newArray.length < this.size) { // 说明没有更多数据
							self.status = 'noMore'
							self.more = false
							self.showEmpty = false
						}
						self.orderList=self.orderList.concat(newArray);
					}
				}).finally(() => {
					setTimeout(()=>{
						this.$tools.hideLoading()
					}, 1000)
				})
			},
		}
	}
</script>

<style lang="scss">
	.content{
		padding: 32upx;
	}
	.nav-box{
		border-bottom: 1rpx solid #36355E;
		padding-bottom: 30upx;
		.nav-item{
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #82819B;
			.line{
				display: inline-block;
				width: 94rpx;
				height: 4rpx;
				margin-top: 10rpx;
			}
		}
		.active{
			color: #fff;
			.line{
				background: #26BCFD;
			}
		}
	}
	.shop-box{
		background: #1D1C5C;
		border-radius: 16rpx;
		margin-top: 36upx;
		.top-box{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			padding: 32upx 28upx;
		}
		.shop-cont{
			padding: 32upx 28upx;
			border-top: 1rpx solid #36355E;
			border-bottom: 1rpx solid #36355E;
			.shop-item{
				margin-bottom: 20upx;
				&:nth-last-of-type(1){
					margin-bottom: 0;
				}
			}
			.shop-img{
				width: 164rpx;
				height: 164rpx;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				flex: none;
			}
			.shop-info-box{
				margin-left: 28upx;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color:#FFFFFF;
				height: 164rpx;
				justify-content: space-between;
				.info-box{
					.specs-box{
						color: #8786A8;
						margin-top: 12upx;
					}
				}
			}
		}
		.total-box{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color:#8786A8;
			line-height: 100upx;
			text-align: right;
			padding: 0 28upx;
			.price{
				color: #FFFFFF;
				margin-left: 24upx;
			}
		}
	}
</style>
